<?php
$isReloadable = false;
require_once("../../pers/inc.config.php");
require_once('../_inc/inc.session.php');
require_once('../_inc/funct.html.php');
require_once('../_inc/funct.docFile.php');
$docType = reqVar('type');
require '../_inc/inc.setDocParams.php';

$sSql = "SELECT d.*, IF(u.id_utente=0,'installer',CONCAT(u.nome,' ',u.cognome)) AS u_nome FROM $tab d"
   ." LEFT JOIN ovf_utenti u ON u.id_utente = d.uploading_user"
   ." WHERE d.id_doc = ".reqVarInt('iddoc');
$objDB->query($sSql,'selYYY');
$acDoc = $objDB->fetch('selYYY');
if (!$acDoc) {
   backOnErr('orderDocMod - id doc '.reqVar('iddoc').' missing');
   die('*** abnormal end ***'); //unreachable
}
$_REQUEST['id'] = $acDoc[$idName];

switch (reqVar('azione')) {
   case 'save':
      $msgPag .= saveDoc($docType,$tab);
      break;
   case 'dele':
      $msgPag .= deleDoc($tab);
      break;
}

$sSql = "SELECT DISTINCT folder FROM $tab "
   ." WHERE $idName IN (0,".$acDoc[$idName].") ORDER BY folder";
$objDB->query($sSql,'selYYY');
$aFolder = array('S'=>'-- no folder --');
while($acF = $objDB->fetch('selYYY')) {
   $fold = $acF['folder'];
   //dbg_($fold);
   if ($fold=='') $fold='S';
   if (!key_exists($fold,$aFolder)) {
      if ($fold[0]=='S') {
         $foldX = 'Specific'.substr($fold,1);
      } else {
         $foldX = 'Global'.substr($fold,1);
      }
      $aFolder[$fold] = $foldX;
   }
}
   
$cmdL = "";

$cmdR = "";
$cmdR .= "<a class=\"cmd1\" href=\"javascript:confDel();\">delete</a>";
$cmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$cmdR .= "<a class=\"cmd1\" href=\"javascript:submAct('save');\">save</a>";
$cmdR .= cmdTorna(); 

testaHtml($t1,$t2.' update',$msgPag,$menuCode);
?>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','...');?>
<?=getHtmlHidden('iddoc');?>
<?=getHtmlHidden('type');?>
<?=getHtmlHidden('xsn',XSN);?>
<script>
   function confDel() {
      if (confirm("Comfirm ?")) {
         submAct('dele');
      }
   }
   function submAct(act) {
      document.frm1.azione.value = act;
      document.frm1.submit();
   }
</script>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?
echo getHtmlRigaRif();
if ($headFunct!='') echo $headFunct(reqVar('id'));
?>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <? echo $cmdL; ?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <? echo $cmdR; ?>
      </td>
   </tr>
   <tr>
      <td class='tit1' colspan='20'>Document</td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Folder</td>
      <td class='txt1' colspan='18'>
         <?
         reqdef('folder',$acDoc['folder']);         
         echo getHtmlSelect('folder',$aFolder);?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Description</td>
      <td class='txt1' colspan='18'>
         <?
         reqdef('description',$acDoc['description']);
         echo getHtmlText('description',64 ,64);?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Note</td>
      <td class='txt1' colspan='18' valign='top'>
         <?
         reqdef('note',$acDoc['note']);
         echo getHtmlTextArea('note',80,2);?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>File</td>
      <td class='txt1' colspan='10' valign='top'>
         <a class='txt1' href='entDocDL.php?type=<?=$docType?>&table=<?=$tab?>&iddoc=<?=$acDoc['id_doc']?>&xsn=<?=XSN?>' target='_blank' 
            title='download document'><?=getHtmlImgDoc($acDoc['file_name']).'&nbsp;&nbsp;'.$acDoc['file_name']?></a>
      </td>
      <td class='txt1' colspan='8' valign='top'>
         <?
         echo "Uploaded by ".$acDoc['u_nome'].' on '.date('d/m/Y H:i:s',strtotime($acDoc['uploading_time']));?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Reloading file</td>
      <td class='txt1' colspan='18' valign='top'>
         <?
         echo getHtmlFile('filedoc',40);?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <?//echo $cmdL; ?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <? echo $cmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function saveDoc($docType,$tab) {
   global $objDB,$objSess;
   $msg = '';

   if ($msg != '') {
      return $msg;
   }
   $sSql = "UPDATE $tab SET description = '".reqVarAS('description')."' "
      .",note = '".reqVarAS('note')."' "
      .",folder = '".reqVarAS('folder')."' "
      ." WHERE id_doc = ".reqVar('iddoc');
   $objDB->exec($sSql);
   if (isset($_FILES['filedoc']['tmp_name']) && $_FILES['filedoc']['tmp_name'] != "") {
      $sFileName = $_FILES['filedoc']['name'];
      $mess = '';
      $rc = putDocFile($docType, $objSess->idUtente, reqVar('iddoc'), $sFileName, $_FILES['filedoc']['tmp_name'],$mess);
      if (!$rc) {
         return $mess;
      }
      $sSql = "UPDATE $tab SET uploading_time = '".date('Y-m-d H:i:s')."' "
         .",uploading_user = '".$objSess->idUtente."' "
         .",file_name = '".addslashes($sFileName)."' "
         ." WHERE id_doc = ".reqVar('iddoc');
      $objDB->exec($sSql);
   }
   backPage('doc upated');
   exit();
}

function deleDoc($tab) {
   global $objDB,$objSess;
   $msg = '';

   if ($msg != '') {
      return $msg;
   }
   $sSql = "DELETE FROM $tab WHERE id_doc = ".reqVar('iddoc');
   $objDB->exec($sSql);
   backPage('doc deleted');
   exit();
}
